package com.bawei.yangzongcai___chenmei.BlankFragment.Blanck2;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bawei.yangzongcai___chenmei.Adpter.QAQadpterActivity4;
import com.bawei.yangzongcai___chenmei.JavaClass.JinxiClass;
import com.bawei.yangzongcai___chenmei.MVC.BsaeFragment;
import com.bawei.yangzongcai___chenmei.R;
import com.bawei.yangzongcai___chenmei.SQL.SQLyongyong2;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;

import java.util.ArrayList;
import java.util.List;


public class BlankFragmentQAQ5 extends BsaeFragment  implements OnItemChildClickListener, OnItemChildLongClickListener {

    SQLiteDatabase reaDatabase;
    private RecyclerView qaq5rv;
    private QAQadpterActivity4 qaqfour;
    List<JinxiClass> jinxiClasses;
    @Override
    public int bandLayout() {
        return R.layout.fragment_blank_fragment_qaq5;
    }

    @Override
    public void initView() {

        qaq5rv = findbandid(R.id.qaq5rv);
        qaq5rv.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));

    }

    @Override
    public void initDate() {

        SQLyongyong2 yongday2 = new SQLyongyong2(getActivity(),"chenchen.dp",null,1);
        reaDatabase = yongday2.getReadableDatabase();
        jinxiClasses = new ArrayList<>();
        Cursor chenchen = reaDatabase.query("chenchen", null, null, null, null, null, null);
        while (chenchen.moveToNext())
        {
            String id = chenchen.getString(chenchen.getColumnIndex("id"));
            String imags = chenchen.getString(chenchen.getColumnIndex("imgs"));
            JinxiClass jinxi = new JinxiClass(id,imags);
            jinxiClasses.add(jinxi);
        }
       qaqfour = new QAQadpterActivity4(jinxiClasses);
        qaqfour.setOnItemChildClickListener(this);
        qaqfour.setOnItemChildLongClickListener(this);
        qaq5rv.setAdapter(qaqfour);

    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId())
        {
            case R.id.kauiqu_img:
                ToastUtils.showShort(""+jinxiClasses.get(position).getId());
                break;
        }
    }




    @Override
    public boolean onItemChildLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, final int position) {
        switch (view.getId())
        {
            case R.id.kauiqu_img:
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setIcon(R.drawable.tubiao_four);
                builder.setMessage("是否确定删除该数据？");
                builder.setTitle("执行删除");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("数据已删除");
                        reaDatabase.execSQL("delete from chenchen where imgs=?",new Object[]{jinxiClasses.get(position).getImags()});
                        jinxiClasses.remove(position);
                        qaqfour.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ToastUtils.showShort("取消了");
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

                break;
        }


        return true;
    }
}
